<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;
use util\Excel;

use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;

use app\admin\model\SystemAdmin;

/**
 * Translate控制器
 */
class Translate extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\Translate();

        $this->validate = \app\admin\validate\Translate::class;
    }

    /**
     * 上传文件
     */
    public function uploads()
    {
        $post = $this->request->post();
        if (empty($post['adminid'])) {
            return error('身份过期请重新登录');
        }

        $file = $this->request->file('file');
        // try {
        $upload = new Upload();
        $res = $upload->upload($file);
        if ($res['url']) {
            // return success('ok', $res['url']);
            $return = $this->import_translate($res['url'], $post['adminid']);
            return success('ok', $return);
        } else {
            return error($res['msg']);
        }
        // } catch (\Exception $e) {
        //     Log::error("---ajax---upload--error:" . $e);
        // }
        return error('上传文件失败');
    }

    //导入交易记录
    public function  import_translate($url, $adminid)
    {
        //查询当前用户上月有没有交易记录，有就删除重新插入，没有就直接插入
        // 获取上个月的开始结束时间
        $start_time = date('Y-m-01', strtotime('-1 month'));
        $end_time = date('Y-m-d', strtotime(-date('d') . 'day'));
        $where[]  = ['create_time', '>=', $start_time];
        $where[]  = ['create_time', '<=', $end_time];
        $last_translate  = table('translate')->where($where)->select()->toarray();
        // halt($last_translate);
        if (!empty($last_translate)) {
            table('translate')->where($where)->where(['admin_id' => $adminid])->delete();
        }
        //获取导入的数据
        $url_arr = explode('storage/', $url);
        $data = Excel::import('storage/' . $url_arr[1], 2);
        foreach ($data as $key => $value) {
            $type = $this->key_site(($key));
            foreach ($value as $kl => $info) {
                if (!empty($info[0])) {
                    $rst[] = [
                        'create_time' => excelTime($info[0]),
                        'product_detail' => $info[2],
                        'product_price' => $info[3],
                        'rebate' => $info[4],
                        'amz_price' => $info[5],
                        'other' => $info[6],
                        'site' => $type,
                        'admin_id' => $adminid,
                    ];
                }
            }
        }
        $this->model->insertAll($rst);
        //更新结算时间
        return $url;
    }
    //通过下标获取对应的站点类型
    public function key_site($key)
    {
        $data = ['uk', 'de', 'fr', 'it', 'es', 'nl', 'se', 'bi', 'pl', 'us', 'tr'];
        return $data[$key];
    }
}
